
//获取应用实例
const app = getApp()

Component({
    data: {
        showTopTips: false,
        mobile: '',

        formData: {

        },
        rules: [{
            name: 'mobile',
            rules: [{ required: true, message: '请输入手机号'}, {mobile: true, message: '手机号格式不对'}],
        }],
        wh:0
    },
    onShow: function () {
      var that = this;
      wx.getSystemInfo({
        success: function (res) {
          that.setData({
            "wh": res.windowHeight - 48 -wx.getSystemInfoSync().statusBarHeight
          })
        }
      })
    },
    methods: {
      goBack: function(){
        wx.navigateBack({
          delta: 1 //返回的页面数，如果 delta 大于现有页面数，则返回到首页,
        });
      },
        formInputChange(e) {
          const { field } = e.currentTarget.dataset
          this.setData({
            [`formData.${field}`]: e.detail.value
          })
        },
        onLoad: function () {
          if (app.globalData.memberInfo) {
            this.setData({
              mobile: app.globalData.memberInfo.tel
            })
          }
          else {
            app.memberInfoReadyCallback = res => {
              this.setData({
                mobile: app.globalData.memberInfo.tel
              })
            }
          }
        },
        submitForm() {
            this.selectComponent('#form').validate((valid, errors) => {
                console.log('valid', valid, errors)
                if (!valid) {
                    const firstError = Object.keys(errors)
                    if (firstError.length) {
                        this.setData({
                            error: errors[firstError[0]].message
                        })
    
                    }
                } else {
                  wx.request({
                    url: 'https://www.xinhuo.site/api/partyMember/changeMobile',
                    data: {
                      id: this.data.memberInfo.id,
                      tel: this.data.formData.mobile
                    },
                    method: 'POST',
                    header: {
                      'Content-Type': 'application/json'
                    },
                    success(res) {
                      if (res.data.syscode == 0) {
                        wx.showToast({
                          title: '修改成功'
                        });
                        wx.navigateBack({
                          delta: 1
                        });
                      }
                      else if (res.data.syscode == 1) {
                        wx.showToast({
                          title: '无法匹配多名用户',
                          icon: 'none'
                        });
                      }
                      else if (res.data.syscode == 2) {
                        wx.showToast({
                          title: '没有匹配的用户',
                          icon: 'none'
                        });
                      }
                      else {
                        wx.showToast({
                          title: '操作失败请联系管理员',
                          icon: 'none'
                        });
                      }
                    }
                  })
                }
            })
            // this.selectComponent('#form').validateField('mobile', (valid, errors) => {
            //     console.log('valid', valid, errors)
            // })
        }

  },
  externalClasses: ["my-page"]
});